package com.jykj.modbus.equipment.controller.guard;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jykj.modbus.equipment.entity.guard.Equipment;
import com.jykj.modbus.equipment.entity.guard.EquipmentMessage;
import com.jykj.modbus.equipment.service.EquipmentService;
import com.jykj.modbus.system.app.HttpResponse;
import com.jykj.util.log.Log;
import org.springframework.data.repository.query.Param;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


public class EquipmentControllerGuard {

	@Resource(name= "equipmentService")
	public EquipmentService service;

	private final static Log log;

	static{
		log = Log.getLog("com.jykj.equipment.controller.equipmentControllerGuard");
	}


	/**
	 * 保存设备信息记录
	 */
	@RequestMapping(value = "/saveEquipment.json")
	@ResponseBody
	public HttpResponse saveEquipment(@RequestBody String jsonDate , HttpServletRequest request, HttpServletResponse response, Model model) {

		HttpResponse ret;
		try {
			Equipment obj = new Equipment();
			JSONObject dataJson= JSONArray.parseObject(jsonDate);
			Map<String,Object> data=(Map<String,Object>)dataJson;
			obj.setSensorType(data.get("sensorType").toString());
			obj.setBaudRate(data.get("baudRate").toString());
			obj.setSerialPort(data.get("serialPort").toString());
			List<Equipment> eqlist = this.service.selectEquipmentList(obj);
			if (eqlist.size()>0){
				return HttpResponse.fail("保存记录重复,数据已存在!","");
			}else {
				this.service.saveEquipment(obj);
				ret = HttpResponse.success("保存记录成功!","");
			}
			//todo 添加日志位置
		} catch (RuntimeException e){
			e.printStackTrace();
			return HttpResponse.fail("记录保存出错!",e);
		} catch (Exception e) {
			log.error("保存记录出错！");
			log.errorPrintStacktrace(e);
			ret = HttpResponse.fail("记录保存出错!",e);
		}
		return ret;
	}

	/**
	 * 保存设备报文信息记录
	 */
	@RequestMapping(value = "/saveEquipmentMessage.json")
	@ResponseBody
	public HttpResponse saveEquipmentMessage(@RequestBody String jsonDate , HttpServletRequest request, HttpServletResponse response, Model model) {

		HttpResponse ret;
		try {
			EquipmentMessage obj = new EquipmentMessage();
			JSONObject dataJson = JSONArray.parseObject(jsonDate);
			Map<String,Object> data=(Map<String,Object>)dataJson;
			//设备码
			obj.setSensorType(data.get("sensorType").toString());
			//报文类型
			obj.setMessageType(Integer.parseInt(data.get("messageType").toString()));
			//报文内容
			obj.setMessage(data.get("message").toString());
			this.service.saveEquipmentMessage(obj);
			//todo 添加日志位置
			ret = HttpResponse.success("保存记录成功!","");
		} catch (RuntimeException e){
			return HttpResponse.fail("记录保存出错!",e);
		} catch (Exception e) {
			log.error("保存记录出错！");
			log.errorPrintStacktrace(e);
			ret = HttpResponse.fail("记录保存出错!",e);
		}
		return ret;
	}

	/**
	 * 修改方法
	 */
	@RequestMapping(value = "/updateEquipment.json")
	@ResponseBody
	public HttpResponse updateEquipment(@RequestBody String jsonDate , HttpServletRequest request ,
										HttpServletResponse response, Model model) {
		HttpResponse ret;
		try {
			//从session中获取当前操作用户信息
//			int userId = 1;
//			if (request.getSession().getAttribute("userId") != null) {
//				userId = (int) Long.parseLong(request.getSession().getAttribute("userId").toString());
//			}
//			BaseLocal local = EquipmentDaoGuard.getThreadLocal();
//			local.setCurrentUserId(userId); //把当前操作人ID存入当前线程对象中
//			EquipmentDaoGuard.setThreadLocal(local);
			JSONObject dataJson = JSONArray.parseObject(jsonDate);
			Map<String,Object> data=(Map<String,Object>)dataJson;
			Equipment obj = new Equipment();
			obj.setBaudRate(data.get("baudRate").toString());
			obj.setId(Integer.parseInt(data.get("id").toString()));
			obj.setIdentificationCode(Integer.parseInt(data.get("identificationCode").toString()));
			obj.setSensorType(data.get("sensorType").toString());
			obj.setSerialPort(data.get("serialPort").toString());
			this.service.updateEquipment(obj);
			//todo 添加日志位置

			ret = HttpResponse.success("修改记录成功!","");
		} catch (RuntimeException e){
			return HttpResponse.fail("记录修改过程超时!",e);
		} catch (Exception e) {
			log.error("修改记录出错！");
			log.errorPrintStacktrace(e);
			ret = HttpResponse.fail("记录修改出错!",e);
		}
		return ret;
	}

	/**
	 * 删除方法
	 */
	@RequestMapping(value="/deleteEquipmentlist.htm")
	public HttpResponse deleteEquipmentList(@Param("organ") Equipment obj, HttpServletRequest request,
									HttpServletResponse response, Model model){
		try{
			int userId = 1;
			if (request.getSession().getAttribute("userId") != null) {
				userId = (int) Long.parseLong(request.getSession().getAttribute("userId").toString());
			}
			this.service.deleteEquipmentByid(obj);
			//todo 操作日志记录
		}catch(Exception e){
			log.error("删除记录出错！");
			log.errorPrintStacktrace(e);
			return HttpResponse.fail("删除记录出错!","");
		}
		return HttpResponse.success("删除记录成功!","");
	}

}

